<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>读取资源流到一个字符串</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.stream-get-contents" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_get_contents</h1> 
  <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">stream_get_contents</span> &mdash; <span class="dc-title">读取资源流到一个字符串</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-get-contents-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_get_contents</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$maxlength</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = -1</span></span>
  ]] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   与 <span class="function"><a href="file_get_contents.html" class="function">file_get_contents()</a></span> 一样，但是
    <span class="function"><strong>stream_get_contents()</strong></span> 是对一个已经打开的资源流进行操作，并将其内容写入一个字符串返回。
   返回的内容取决于 <code class="parameter">maxlength</code> 字节长度和 <code class="parameter">offset</code> 指定的起始位置。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-get-contents-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">handle</code> (<span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>)</dt>

     <dd>

      <p class="para">
       一个资源流（例如 <span class="function"><a href="fopen.html" class="function">fopen()</a></span> 操作之后返回的结果）
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">maxlength</code> (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)</dt>

     <dd>

      <p class="para">
       需要读取的最大的字节数。默认是-1（读取全部的缓冲数据）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">offset</code> (<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)</dt>

     <dd>

      <p class="para">
       在读取数据之前先查找指定的偏移量。如果这个数字是负数，就不进行查找，直接从当前位置开始读取。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-get-contents-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个字符串  或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-get-contents-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.1.0</td>
       <td>
        增加参数 <code class="parameter">offset</code> 。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-get-contents-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4823">
    <p><strong>Example #1 <span class="function"><strong>stream_get_contents()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;print&nbsp;all&nbsp;the&nbsp;page&nbsp;starting&nbsp;at&nbsp;the&nbsp;offset&nbsp;10<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br />}<br /><br /><br />if&nbsp;(</span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.net'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;print&nbsp;the&nbsp;first&nbsp;5&nbsp;bytes<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.stream-get-contents-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数可安全用于二进制对象。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-get-contents-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
    <li class="member"><span class="function"><a href="fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - 读取文件（可安全用于二进制文件）</span></li>
    <li class="member"><span class="function"><a href="fpassthru.html" class="function" rel="rdfs-seeAlso">fpassthru()</a> - 输出文件指针处的所有剩余数据</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>